package com.zcs.app.lucky.ui

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import cn.sxw.android.base.adapter.CommonRecyclerAdapter
import cn.sxw.android.base.kt.BaseViewModel
import cn.sxw.android.base.utils.GlideStaticUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.safframework.delegate.extras.extraDelegate
import com.zcs.app.lucky.R
import com.zcs.app.lucky.base.BaseLuckyActivity
import com.zcs.app.lucky.data.GroupDataCache
import com.zcs.app.lucky.databinding.ActivityGroupListBinding
import com.zcs.app.lucky.entity.GroupEntity
import com.zcs.app.lucky.entity.KEY_FOR_CHECK
import java.util.*

/**
 * 分组列表
 */
class GroupListActivity : BaseLuckyActivity<ActivityGroupListBinding, BaseViewModel>() {
    private var mAdapter: CommonRecyclerAdapter<GroupEntity>? = null
    private val mItems = ArrayList<GroupEntity>()
    private val forCheck by extraDelegate(KEY_FOR_CHECK, false)

    // 懒加载机制
    override fun onReady() {
        binding.toolbar.tvToolbarTitle.text = "人员分组列表"
        binding.loadingLayout.visibility = View.VISIBLE
        binding.toolbar.tvToolbarRight.text = "新建"
    }

    override fun onInit() {
        initView()
        initObserve()
        initData()
    }

    override fun initView() {
    }

    override fun initObserve() {
    }

    override fun initData() {
        mItems.clear()
        val list = GroupDataCache.loadAllGroups()
        if (list.isNotEmpty()) {
            mItems.addAll(list)
            binding.tvTips.visibility = View.GONE
            binding.btnCreateNow.visibility = View.GONE
        } else {
            binding.tvTips.visibility = View.VISIBLE
            binding.btnCreateNow.visibility = View.VISIBLE
        }
        initAdapter()
        hideLoading()
    }

    private fun initAdapter() {
        if (mAdapter == null) {
            mAdapter =
                object : CommonRecyclerAdapter<GroupEntity>(R.layout.item_group_row, mItems) {
                    override fun convert(holder: BaseViewHolder, item: GroupEntity) {
                        holder.setText(R.id.tvName, item.niceName())
                        holder.setText(R.id.tvDesc, item.niceDesc())
                        // 头像列表
                        val avatarList = listOf<ImageView>(
                            holder.getView(R.id.ivAvatar3),
                            holder.getView(R.id.ivAvatar2),
                            holder.getView(R.id.ivAvatar1)
                        )
                        for (avatar in avatarList) {
                            avatar.setImageResource(0)
                            avatar.visibility = View.INVISIBLE
                        }

                        val list = item.list
                        if (!list.isNullOrEmpty()) {
                            for ((i, iv) in avatarList.withIndex()) {
                                if (list.size <= i)
                                    break
                                iv.visibility = View.VISIBLE
                                GlideStaticUtils.displayCircleImage(
                                    list[i].avatar, 0, iv
                                )
                            }
                        }
                    }
                }
            binding.rvUsers.adapter = mAdapter
            mAdapter!!.setOnItemClickListener { _, _, position ->
                val clickItem = mItems[position]
                onCreateGroup(clickItem.id)
            }
        } else {
            mAdapter!!.notifyDataSetChanged()
        }
    }


    override fun hideLoading() {
        super.hideLoading()
        binding.loadingLayout.visibility = View.GONE
    }

    override fun addOnClickListener() {
        binding.toolbar.ivToolbarBack.setOnClickListener { onBackPressed() }
        // 创建分组
        binding.toolbar.tvToolbarRight.setOnClickListener {
            onCreateGroup()
        }
        binding.btnCreateNow.setOnClickListener {
            onCreateGroup()
        }
    }

    private fun onCreateGroup(id: String = "") {
        if (forCheck && id.isNotEmpty()) {
            GroupDataCache.tempCheckedId.postValue(id)
            killMyself()
            return
        }
        goAdd = true
        if (TextUtils.isEmpty(id)) {
            launchActivity(CreateGroupActivity::class.java)
        } else {
            val intent = Intent(this, CreateGroupActivity::class.java)
            intent.putExtra(CreateGroupActivity.KEY_GROUP_ID, id)
            launchActivity(intent)
        }
    }
}